<?
echo "*";
$sources = file('./wgets/sourcesB.txt');
echo count($sources)."<br>";

while(list($skey,$sval)=each($sources)){
	if($sval!='\n'){
		$fval = './wgets/'.trim(str_replace('http://','',$sval)).'index.html';
		//echo " ::".$MYud."<br><a href=\"".$fval."\" target=\"_new\">".$fval."</a>";
		$MYud = '';
			$dataf = file($fval);
			$val = join ('',$dataf);

		//echo substr_count($val,"'/s/")."<br>";
		if(stristr($val,"'/s/")){
			$valA = explode("'/s/",$val);
			//echo count($valA)."<br>";
			//echo htmlspecialchars($valA[0]);
			
			for($i = 1; $i <count($valA); $i++){
					$val2 = $valA[$i];
					if(stristr($val2,"/1/'>")){
						$collection = $collection +1;
						$v2A = explode("/1/'>",$val2);
								$dataA[$collection]['id'] = $v2A[0];
						$t = explode('</a>',$v2A[1]);
							$dataA[$collection]['title'] = $t[0];
							//echo $t[0];
							$t2 =explode('/u/',$t[1]);
							$t3 =explode("/'>",$t2[1]);
								$dataA[$collection]['author_id'] = $t3[0];
								$dataA[$collection]['author'] = $t3[1];
						$v3A = explode("<blockquote>",$val2);		
						$t2 = explode('</blockquote',$v3A[1]);
						//echo htmlspecialchars($t2[0])."<hr>";
						
						
						
						$t3 = explode("<br><span class='gray' style='font-size=11px'>", $t2[0]);
							$dataA[$collection]['summary'] = trim($t3[0]);
							//echo htmlspecialchars(trim($t3[0]));
						$t4 = explode(" - ", $t3[1]);
						$dataA[$collection]['chapters'] = str_replace('Chapters: ','',$t4[3]);
						$ud1 = str_replace('Updated: ','',$t4[6]);
						$pd1 = strip_tags(str_replace('Published: ','',$t4[7]));
						$ud2 = explode('-',$ud1);
						if(strlen($ud2[0])==1){ $ud2a='0'; }else{ $ud2a='';}
						if(strlen($ud2[1])==1){ $ud2b='0'; }else{ $ud2b='';}
						
						$ud3 = '20'.$ud2[2].'-'.$ud2a.$ud2[0].'-'.$ud2b.$ud2[1];
						$pd2 = explode('-',$pd1);
						if(strlen($pd2[0])==1){ $pd2a='0'; }else{ $pd2a='';}
						if(strlen($pd2[1])==1){ $pd2b='0'; }else{ $pd2b='';}
						$pd3 = '20'.$pd2[2].'-'.$pd2a.$pd2[0].'-'.$pd2b.$pd2[1];
						$dataA[$collection]['updated'] = $ud3;
						$MYud = $ud3;
						if($ud3<$aDT[$cat] && $ud3!='20--'){
							$pageL=false;
						}
						$dataA[$collection]['published'] = $pd3;
						/*
						*/
					
					}			
			}
		}

	}
}



echo "<hr>";
echo "Fics Found: ".count($dataA);
/*
echo "<pre>";
var_dump($dataA);
echo "</pre>";
*/
require('/home/httpd/html/adodb/adodb.inc.php');

$db = &ADONewConnection('mysql');
$db->PConnect('localhost','root','ttshb**','lpreston');

//$sqLST = "SELECT story_id,chapters,updated,status,summary,author_id,published,story_title FROM fic_listing WHERE sitekey='F' AND status !='I'";
$sqLST = "SELECT story_id,chapters,updated,status,summary,author_id,published,story_title FROM fic_listing WHERE sitekey='F' ";
$rsLST = $db->Execute($sqLST);
$aLST = $rsLST->GetAssoc();
$found = count($dataA);
$poss = count($aLST);
while(list($key,$val)=each($dataA)){

	if(isset($aLST[$val['id']])){		
				$upd = false;
				if($aLST[$val['id']]['chapters']!=$val['chapters']){ $upd = true; }
				if($aLST[$val['id']]['updated']!=$val['updated']){ $upd = true; }
				$cnta=$cnta+1;
				
				if($upd){
					$adQ = '';
					$sqT = "chapters='".$val['chapters']."',updated='".$val['updated']."',inserted=NOW()";
					if($aLST[$val['id']]['published']=='0000-00-00'){
						$sqT .= ", published='".$val['published']."'";						
					}
					if($aLST[$val['id']]['author_id']=='0'){
						$sqT .= ", author_id='".$val['author_id']."'";						
					}
					if($aLST[$val['id']]['summary']!=$val['summary']){
						$sqT .= ", summary='".str_replace("'","''",$val['summary'])."'";	
					}	
					if($aLST[$val['id']]['story_title']!=$val['title']){
						$sqT .= ", story_title='".str_replace("'","''",$val['title'])."'";	
					}
						//echo $sqT."<br>";
					

					if($val['updated']!='20--'){					
						if($aLST[$val['id']]['status']=='?' || $aLST[$val['id']]['status']=='N'){
							$cnt=$cnt+1;
							$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='F' and story_id='".$val['id']."'";
							
							$ffUD = $ffUD+1;
						}elseif($aLST[$val['id']]['status']!='I' && $aLST[$val['id']]['status']!='B' && $aLST[$val['id']]['status']!='D' ){	
							$cnt=$cnt+1;
							$sqlU = "UPDATE fic_listing SET ".$sqT.",status='U' WHERE sitekey='F' and story_id='".$val['id']."'";						
							
							$ffUD = $ffUD+1;
						}elseif($aLST[$val['id']]['status']=='I' || $aLST[$val['id']]['status']=='B'){	
							if( $aLST[$val['id']]['status']=='B'){
								$sqlU = "UPDATE fic_listing SET ".$sqT.",status='D' WHERE sitekey='F' and story_id='".$val['id']."'";							
								$cnt1=$cnt1+1;
							}else{
								$sqlU = "UPDATE fic_listing SET ".$sqT.",status='D' WHERE sitekey='F' and story_id='".$val['id']."'";						
							}							
							$ffUD = $ffUD+1;
						}
						
						$db->Execute($sqlU);
						
						
						//echo $val['title']." (".$val['updated'].")<br>";
					}else{
						//echo "<i>".$val['title']." (".$val['updated'].")</i><br>";
					}
				}else{
					//echo $val['title']." -".$aLST[$val['id']]['updated']."-".$val['updated']."<br>";
				}	
	}else{
		//echo $val['id']." -- ".$val['title']."<br>";
		//$cnt2 = $cnt2 + 1;
	}
}

echo "<br>Known Fics: ".$cnta."<br>&nbsp;&nbsp;Updated Choice: ".$cnt."<br>&nbsp;&nbsp;Updated Other: ".$cnt1."<br>New Fics: ".$cnt2;
?>
